package com.qin.controller;

import com.alibaba.fastjson.JSONObject;
import com.qin.entity.Component;
import com.qin.entity.ExecuteBean;
import com.qin.enums.RetCode;
import com.qin.service.ComponentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;

@Controller("componentController")
@RequestMapping("/component")
@ResponseBody
public class ComponentController {

    @Autowired
    private ComponentService componentService;

    @RequestMapping(value="/executeComponent", consumes="application/json;charset=UTF-8", produces="application/json;charset=UTF-8")
    public String executeComponent(@RequestBody String body){
//        System.out.println("web层：executeComponent");
        System.out.println("接口调用：/component/executeComponent");

        //入参封装到实体类
        ExecuteBean executeBean = JSONObject.parseObject(body, ExecuteBean.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((executeBean.getComponent_id() == null) || ("".equals(executeBean.getComponent_id()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "component_id不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }
        //调用service层
        String retString = componentService.executeComponent(executeBean).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }
}
